package thinkinginjava.concurrency;

public class SynchronizeDemo implements Runnable {

	static SynchronizeDemo inst = new SynchronizeDemo();

	public static SynchronizeDemo getIns() {
		return inst;
	}

	static Object o = new Object();

	public void method1() {
		SynchronizeDemo d1 = new SynchronizeDemo();
		Thread t1 = new Thread(d1);
		t1.start();
	}

	public synchronized void method2() {
		synchronized (o) {
			System.out.println("method2");
		}

	}

	public static void main(String[] args) {
		// SynchronizeDemo d1 = new SynchronizeDemo();
		// SynchronizeDemo d2 = new SynchronizeDemo();
		// Thread t1 = new Thread(d1);
		// Thread t2 = new Thread(d2);
		// t1.start();
		// t2.start();
		SynchronizeDemo d1 = new SynchronizeDemo();
		d1.method1();
		d1.method1();
	}

	@Override
	public void run() {
		print();
	}

	private void print() {
		synchronized (o) {
			System.out.println(Thread.currentThread() + "method1");
		}
	}
}
